﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.model;
using l1cs.server.datatables;
using l1cs.server.templates;

namespace l1cs.server.clientpackets
{
    public class C_BoardWrite : ClientBasePacket
    {
        private const String _C_BOARD_WRITE = "[C] C_BoardWrite";
        private static Logger _log = Logger.getLogger( typeof( C_BoardWrite ).FullName );

        public C_BoardWrite( byte[] decrypt, ClientThread client )
            : base( decrypt )
        {
            int id = readD();
            L1Board board = new L1Board();
            board.date = currentTime();
            board.title = readS();
            board.content = readS();

            L1Object tg = L1World.getInstance().findObject( id );

            if ( tg != null )
            {
                L1PcInstance pc = client.getActiveChar();
                pc.getInventory().consumeItem( L1ItemId.ADENA, 300 );
                BoardTable.getInstance().writeTopic( pc, board );
            }
            else
            {
                _log.warning( "不正なNPCID : " + id );
            }
        }

        private static String currentTime()
        {
            DateTime dateTime = DateTime.Now;
            int year = dateTime.Year - 2000;
            String year2;
            if ( year < 10 )
            {
                year2 = "0" + year;
            }
            else
            {
                year2 = Convert.ToString( year );
            }
            String Month2 = dateTime.Month.ToString("HH");
            String date2 = dateTime.Day.ToString( "dd" );

            return year2 + "/" + Month2 + "/" + date2;
        }

        override
        public String getType()
        {
            return _C_BOARD_WRITE;
        }
    }
}